package com.aier.proxy.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.common.CommonSession;
import com.aier.common.common.Constant;
import com.aier.proxy.service.IUserCreateSchoolService;
import com.aier.school.entity.School;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.service.ISchoolService;
import com.aier.school.service.ISchoolTeacherService;
import com.aier.user.entity.User;
import com.aier.user.service.IUserService;

@Scope("prototype")
@Component("proxyCreateSchoolAction")
public class ProxyCreateSchoolAction extends ProxyBaseAction {
    /**
     * 创建学校
     */
    private static final long serialVersionUID = 1L;
    
    @Resource(name = "userCreateSchoolService")
    IUserCreateSchoolService userCreateSchoolService;
    
    @Resource(name = "userService")
    IUserService userService;
    
    @Resource(name = "schoolService")
    ISchoolService schoolService;
    
    @Resource(name = "schoolTeacherService")
    ISchoolTeacherService schoolTeacherService;

    // 学校名称
    private String scName;
    // 管理员账号
    private String scAccount;
    // 省
    @SuppressWarnings("unused")
    private String province;
    // 市
    @SuppressWarnings("unused")
    private String city;
    //城市id
    private Integer city_id;
    // 区
    @SuppressWarnings("unused")
    private String district;
    // 街
    @SuppressWarnings("unused")
    private String street;
    // 学校地址
    private String address;
    // 网址
    private String website;
    // 联系电话
    private String tel;
    // 邮箱
    private String email;
    // 学校简介
    private String desinfo;
    //状态
    private int s;
    private String error_info;
    
    //代理商id
    private Integer pid;
    
    //代理商城市id
    private int pcid;
    
    // 代理商id与名称
    private List<String[]> tl;

    @SuppressWarnings("unchecked")
    @Override
    public String execute() {
        tl = (List<String[]>) session
                .get(CommonSession.SESSION_ACCOUNT_IDENTITY_PROXY_LIST);
        for(int i=0;i<tl.size();i++){
            if(pid==Integer.valueOf(tl.get(i)[0])){
                pcid = Integer.valueOf(tl.get(i)[2]);
            }
            
        }
        return SUCCESS;
    }

    /**
     * 创建学校
     * @return 
     */
    public String psc_create() {
        s = 0;
        Timestamp ctime = new Timestamp(new Date().getTime());
        User u = userService.searchUserByCondition(scAccount);
        if(u==null){
            error_info = "该管理员账号不存在";
            return SUCCESS;
        }else if(u.getIsbaby()==(short)1){
            error_info = "该管理员账号非注册账号";
            return SUCCESS;
        }
        int school_num = schoolService.countSchoolByName(scName, null);
        if (school_num > 0) {
            error_info = "学校名已经存在";
            return SUCCESS;
        }
        School sc = new School(scName, pid, city_id, desinfo);
        Integer sid = schoolService.insertSchoolNew(sc);
        SchoolTeacher sTeacher = new SchoolTeacher(null, u.getUid(), sid, u.getNickname(), null, ctime, (short)1); 
        s = schoolTeacherService.insertSchoolTeacher(sTeacher);
            
        return SUCCESS;
        
    }
    /**
     * 将要添加为学校管理员的用户名检查
     * @return 10000用户不存在。10001成功
     */
    public String pcs_check() {
        if(null != userService.searchUserByCondition(scAccount)){
            s=Constant.num_10001;
        }else{
            s=Constant.num_10000;
        }
        
        return SUCCESS;
    }

    public int getS() {
        return s;
    }

    public void setScName(String scName) {
        this.scName = scName;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public void setDistrict(String district) {
        this.district = district;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setCity_id(Integer city_id) {
        this.city_id = city_id;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public void setUserCreateSchoolService(
            IUserCreateSchoolService userCreateSchoolService) {
        this.userCreateSchoolService = userCreateSchoolService;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public Integer getPid() {
        return pid;
    }

    public void setScAccount(String scAccount) {
        this.scAccount = scAccount;
    }

    public int getPcid() {
        return pcid;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public void setDesinfo(String desinfo) {
        this.desinfo = desinfo;
    }

    public void setSchoolTeacherService(ISchoolTeacherService schoolTeacherService) {
        this.schoolTeacherService = schoolTeacherService;
    }

    public String getError_info() {
        return error_info;
    }


}
